package chen;

import chen.valuebean.GoodsSingle;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet(name = "ShowServlet", urlPatterns = {"/Show"})
public class ShowServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException
{
    HttpSession session = request.getSession();
    response.setContentType("text/html;charset=UTF-8");
    try (PrintWriter out = response.getWriter())
    {           
      out.println("<!DOCTYPE html>");
      out.println("<html>");
      out.println("<head>Show All Goods For Sale.</head><body>");         
      ArrayList goodslist=(ArrayList)session.getAttribute("goodslist");	      
      out.write("<table border=\"1\" width=\"450\" rules=\"none\" cellspacing=\"0\" cellpadding=\"0\">\n");
      out.write("\t<tr height=\"50\"><td colspan=\"4\" align=\"center\">提供商品如下</td></tr>\n");
      out.write("\t<tr align=\"center\" height=\"30\" bgcolor=\"lightgrey\">\n");
      out.write("\t\t<td>图片</td>\n");
      out.write("\t\t<td>名称</td>\n");
      out.write("\t<td>价格(元/斤)</td>\n");
      out.write("\t\t<td>购买</td>\n");
      out.write("\t</tr>\n");
      out.write("\t");
    if(goodslist==null||goodslist.size()==0)
    { 
      out.write("\n");
      out.write("\t<tr height=\"100\"><td colspan=\"4\" align=\"center\">没有商品可显示！</td></tr>\n");
      out.write("\t");
    } 
   else
    {
      for(int i=0;i<goodslist.size();i++)
      {
	    GoodsSingle single=(GoodsSingle)goodslist.get(i);
            out.write("\n");
            out.write("\t<tr height=\"50\" align=\"center\">\n");
            out.write("\t\t<td><img style='width:50px;' src='");
            out.print(single.getImgurl());
            out.write("'></td>\n");
            out.write("\t\t<td>");
            out.print(single.getName());
            out.write("</td>\n");
            out.write("\t\t<td>");
            out.print(single.getPrice());
            out.write("</td>\n");
            out.write("\t\t<td><a href=\"Docar?action=add&id=");
            out.print(i);
            out.write("\">加入购物车</a></td>\n");
            out.write("\t</tr>\n");
            out.write("\t");
	}
    }
    out.write("\n");
    out.write("\t<tr height=\"50\">\n");
    out.write("<td align=\"center\" colspan=\"4\"><a href=\"Shopcar\">查看购物车</a></td>\n");
    out.write("</tr>\n");
    out.write("</table>\n");
    out.println("</body>");
    out.println("</html>");
    }
}
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}